library(tidyverse)
library(GGally)
library(scales)
library(sf)
library(ggpubr)
# Figure 1: Time Trend and Spatial Distribution of Dismissed Centrally-Managed Officials
# Left Panel
probe_wang = read_rds("probe_wang.rds")
py = read_csv("province_py_list.csv") %>% 
  mutate(c_gbcode = as.character(c_gbcode))

# Figure 1 Stylized Facts of AC Campaign####
# Time Trend
time_trend = probe_wang %>% 
  group_by(year) %>% 
  summarise(purge_cen = sum(purge_cen)) %>% 
  ggplot( aes(x =  year, y = purge_cen )) +
  geom_line() +
  geom_point(size =2) +
  scale_x_continuous(breaks= pretty_breaks()) +
  geom_rect(aes(xmin=2012.5, xmax=Inf, ymin=-Inf, ymax=Inf), alpha =.05) +
  ylab("Number of Purged CM officials") +
  xlab("Year") +
  theme_bw(15)

probe = probe_wang %>% 
  filter(year>2013) %>% 
  mutate(province_id = as.numeric(province_id)) %>% 
  group_by(province_id) %>% 
  summarise(n = sum(purge_cen) %>% as.numeric())

path = "./map/bou2_4p.shp"


china_map <- st_read(path,quiet = T) %>%
  st_sf() %>%
  left_join(probe, by = c("ADCODE99" = "province_id")) 


map =   ggplot(china_map) +
  geom_sf(aes(fill = n) , alpha = .8, color = "grey",size =.1) +
  scale_fill_gradient(name="Purges of CM Officials",
                      low = "ghostwhite",high = "steelblue",
                      na.value="black",
                      breaks = c(2,4,6,8,10,12)) +
  coord_sf(ylim = c(18,52)) +
  theme_minimal(15) +
  theme(legend.position="bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        # panel.grid.minor = element_line(color = "#ebebe5", size = 0.2),
        panel.border = element_blank()    )

map
ggarrange(time_trend,map)

ggsave("/Users/zerenli1992/Dropbox/Apps/Overleaf/li_manion_2020/spatial.jpg")
